<?php

class EmailTemplateController extends GxController {

    public function filters() {
        return array('rights');
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'EmailTemplate'),
        ));
    }

    public function actionCreate() {
        $model = new EmailTemplate;

        $this->performAjaxValidation($model, 'email-template-form');

        if (isset($_POST['EmailTemplate'])) {
            $model->setAttributes($_POST['EmailTemplate']);

            if ($model->save()) {
                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'EmailTemplate');

        $this->performAjaxValidation($model, 'email-template-form');

        if (isset($_POST['EmailTemplate'])) {
            $model->setAttributes($_POST['EmailTemplate']);

            if ($model->save()) {
                $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'EmailTemplate')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('EmailTemplate');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin() {
        $model = new EmailTemplate('search');
        $model->unsetAttributes();

        if (isset($_GET['EmailTemplate']))
            $model->setAttributes($_GET['EmailTemplate']);
        $statuschangeurl = Yii::app()->createUrl('emailTemplate/statuschange/');
        $this->render('admin', array(
            'model' => $model,
            'statuschangeurl' => $statuschangeurl,
        ));
    }

    public function actionStatuschange() {

        $model = $this->loadModel($_POST['id'], 'EmailTemplate');
        $model->status = $_POST['status'];
        $model->save();
    }

}